package com.hillky.emos.wx.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.hillky.emos.wx.config.SystemConstants;
import com.hillky.emos.wx.config.shiro.JwtUtil;
import com.hillky.emos.wx.controller.form.CheckinForm;
import com.hillky.emos.wx.controller.form.SearchMonthCheckinForm;
import com.hillky.emos.wx.exception.EmosException;
import com.hillky.emos.wx.service.TbCheckinService;
import com.hillky.emos.wx.service.TbUserService;
import com.hillky.emos.wx.utils.QCloudCosUtils;
import com.hillky.emos.wx.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;

@RestController
@Api("签到模块web接口")
@Slf4j
public class CheckinController {

    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private TbCheckinService checkinService;

    @Autowired
    private QCloudCosUtils qCloudCosUtils;

    @Autowired
    private SystemConstants constants;

    @Autowired
    private TbUserService userService;


    @GetMapping("/validCanCheckIn")
    @ApiOperation("查看用户今天是否可以签到")
    public R validCanCheckIn(@RequestHeader("token") String token) {
        int userId = jwtUtil.getUserId(token);
        String res = checkinService.validCanCheckIn(userId, DateUtil.today());
        return R.ok(res);
    }

    @PostMapping("/checkin")
    @ApiOperation("签到")
    public R checkIn(@Valid CheckinForm form, @RequestParam("photo") MultipartFile file, @RequestHeader("token") String token) {
        //1.上传图片到云服务器上
        String url="";
         url = qCloudCosUtils.upload(file);
        //2.调用签到方法
        HashMap param = new HashMap<>();
        int userId = jwtUtil.getUserId(token);
        param.put("userId",userId);
        param.put("photo",url);
        param.put("city",form.getCity());
        param.put("district",form.getDistrict());
        param.put("address",form.getAddress());
        param.put("country",form.getCountry());
        param.put("province",form.getProvince());
        checkinService.checkin(param);
        return R.ok("签到成功");
    }

    @GetMapping("/searchTodayCheckin")
    @ApiOperation("查询用户当日签到数据")
    public R searchTodayCheckin(@RequestHeader("token")String token){
        int userId = jwtUtil.getUserId(token);
        HashMap map = checkinService.searchTodayCheckin(userId);
        map.put("attendanceTime",constants.attendanceTime);
        map.put("closingTime",constants.closingTime);
        long days=checkinService.searchCheckinDays(userId);
        map.put("checkinDays",days);

        //入职日期在考勤周期之后
        DateTime hiredate=DateUtil.parse(userService.searchHiredate(userId));
        DateTime startDate=DateUtil.beginOfWeek(DateUtil.date());
        if(startDate.isBefore(hiredate)){
            startDate=hiredate;
        }
        DateTime endDate=DateUtil.endOfWeek(DateUtil.date());
        HashMap param=new HashMap();
        param.put("startDate",startDate.toString());
        param.put("endDate",endDate.toString());
        param.put("userId",userId);
        ArrayList<HashMap> list=checkinService.searchWeekCheckIn(param);
        map.put("weekCheckin",list);
        return R.ok().put("result",map);
    }

    @PostMapping("/searchMonthCheckin")
    @ApiOperation("查询用户某月签到数据")
    public R searchMonthCheckin(@Valid @RequestBody SearchMonthCheckinForm form, @RequestHeader("token")String token){
        int userId=jwtUtil.getUserId(token);
        String hiredate = userService.searchHiredate(userId);
        DateTime hiredateTime = DateUtil.parse(hiredate);
        String month=form.getMonth()<10?"0"+form.getMonth():form.getMonth().toString();
        DateTime startDate=DateUtil.parse(form.getYear()+"-"+month+"-01");
        if(startDate.isBefore(DateUtil.beginOfMonth(hiredateTime))){
            //查询日期早于入职日期当月的第一天
            throw new EmosException("只能查询考勤之后日期的数据");
        }
        if(startDate.isBefore(hiredateTime)){
            //查询日期早于入职日期
            startDate=hiredateTime;
        }
        DateTime endDate = DateUtil.endOfMonth(startDate);
        HashMap param=new HashMap();
        param.put("userId",userId);
        param.put("startDate",startDate.toString());
        param.put("endDate",endDate.toString());
        ArrayList<HashMap> list = checkinService.searchWeekCheckIn(param);
        int sum_1=0,sum_2=0,sum_3=0;
        for (HashMap<String,String> one : list) {
            String type = one.get("type");
            String status = one.get("status");
            if("工作日".equals(type)){
                if("正常".equals(status)){
                    sum_1++;
                }
                else if("迟到".equals(status)){
                    sum_2++;
                }
                else if("缺勤".equals(status)){
                    sum_3++;
                }
            }
        }
        return R.ok().put("list",list).put("sum_1",sum_1).put("sum_2",sum_2).put("sum_3",sum_3);
    }

}
